package com.example.module_home.Module.CodeBank;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.example.module_base.Net.HttpClient;
import com.example.module_base.Net.ResponseHandler;
import com.example.module_home.Entity.CodeInfo;
import com.example.module_home.Entity.CodeTree;
import com.example.module_home.Entity.SearchInfo;
import com.example.module_home.Entity.UserOperationInfo;
import com.example.module_home.Interface.IHomeServer;
import com.example.module_home.Public.Constant;
import com.example.module_home.Utils.DialogUtil;
import com.timmy.tdialog.TDialog;

import java.io.IOException;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;


/**
 * Created by Administrator on 2018/4/24 0024.
 */

/**
 * 代码库接口管理
 */
public class CodeBankManager{
    private static final String TAG = "CodeBankManager";
    private Context mContext;
    @SuppressLint("StaticFieldLeak")
    private static volatile CodeBankManager mInstance = null;
    private String mUser;
    private String mPassword;

    private CodeBankManager(Context context) {
        this.mContext=context;
    }

    public static CodeBankManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (CodeBankManager.class) {
                if (mInstance == null) {
                    mInstance = new CodeBankManager(context);
                }
            }
        }
        return mInstance;
    }

    public interface CodeBankBack<T>{
        void successful(T t);
        void failed(int code, String e);
    }
    /**
     * 获取登陆用户信息
     * @param user 帐号名
     * @param password 密码
     * @param back 回调接口
     */
    public void getLoginUserInfo(String user, String password, @Nullable final TDialog waitDialog, final CodeBankBack<String> back){
        Constant.user = mUser = user;
        Constant.password = mPassword = password;
        isShowDialog(waitDialog);
        HttpClient<IHomeServer> httpClient = new HttpClient<>(mContext,IHomeServer.class);
        Call<String> loginUserInfo = httpClient.service(Constant.baseCodeUrl).getLoginUserInfo(user, password);
        httpClient.request(loginUserInfo,new ResponseHandler<String>(){
            @Override
            public void onSuccess(String s) {
                isDismissDialog(waitDialog);
                back.successful(s);
            }

            @Override
            public void onFailure(int code, String e) {
                isDismissDialog(waitDialog);
                back.failed(code, e);
            }
        });
    }

    /**
     * 获取最近操作信息
     */
    public void getAllOperations(@Nullable final TDialog waitDialog, final CodeBankBack<List<UserOperationInfo>> back){
        isShowDialog(waitDialog);
        HttpClient<IHomeServer> httpClient = new HttpClient<>(mContext,IHomeServer.class);
        Call<List<UserOperationInfo>> operationInfo = httpClient.service(Constant.baseCodeUrl).getAllOperation(mUser, mPassword);
        httpClient.request(operationInfo, new ResponseHandler<List<UserOperationInfo>>() {
            @Override
            public void onSuccess(List<UserOperationInfo> info) {
                isDismissDialog(waitDialog);
                back.successful(info);
            }

            @Override
            public void onFailure(int code, String e) {
                isDismissDialog(waitDialog);
                back.failed(code,e);
            }
        });
    }

    private void isShowDialog(@Nullable TDialog waitDialog){
        if(waitDialog!=null){
            waitDialog.show();
        }
    }
    private void isDismissDialog(@Nullable TDialog waitDialog){
        if(waitDialog!=null){
            waitDialog.dismiss();
        }
    }

    /**
     * 获取文章内容
     * @param codeId 文章ID
     */
    public void getCodeInfo(int codeId,@Nullable final TDialog waitDialog, final CodeBankBack<CodeInfo> back){
        isShowDialog(waitDialog);
        HttpClient<IHomeServer> httpClient = new HttpClient<>(mContext,IHomeServer.class);
        Call<CodeInfo> codeInfo = httpClient.service(Constant.baseCodeUrl).getCodeInfo(mUser,mPassword,codeId);
        httpClient.request(codeInfo, new ResponseHandler<CodeInfo>() {
            @Override
            public void onSuccess(CodeInfo info) {
                isDismissDialog(waitDialog);
                back.successful(info);
            }

            @Override
            public void onFailure(int code, String e) {
                isDismissDialog(waitDialog);
                back.failed(code, e);
            }
        });

    }

    /**
     * 获取代码库树状图
     */
    public void getCodeTree(final CodeBankBack<List<CodeTree>> back){
        HttpClient<IHomeServer> httpClient = new HttpClient<>(mContext,IHomeServer.class);
        Call<List<CodeTree>> codeTree = httpClient.service(Constant.baseCodeUrl).getCodeTree(mUser,mPassword);
        httpClient.request(codeTree, new ResponseHandler<List<CodeTree>>() {
            @Override
            public void onSuccess(List<CodeTree> codeTree) {
                back.successful(codeTree);
            }
            @Override
            public void onFailure(int code, String e) {
                back.failed(code,e);
            }
        });
    }

    /**
     * 获取搜素内容
     * @param title 要输入的内容
     * @param typeSelect 树状图顶层类型选择
     */
    public void getSearchInfo(String title,String typeSelect,@Nullable final TDialog waitDialog,final CodeBankBack<List<SearchInfo>> back){
        isShowDialog(waitDialog);
        HttpClient<IHomeServer> httpClient = new HttpClient<>(mContext,IHomeServer.class);
        Call<List<SearchInfo>> searchInfo = httpClient.service(Constant.baseCodeUrl).getSearchInfo(mUser,mPassword,title,typeSelect);
        httpClient.request(searchInfo, new ResponseHandler<List<SearchInfo>>() {
            @Override
            public void onSuccess(List<SearchInfo> searchInfo) {
                isDismissDialog(waitDialog);
                back.successful(searchInfo);
            }

            @Override
            public void onFailure(int code, String e) {
                isDismissDialog(waitDialog);
                back.failed(code,e);
            }
        });
    }
}
